const ProjectJson = require('../mongod/project_json');
const fs = require('fs');
const { gridFs } = require('../utils/mongod_util');
const logUtil = require('../utils/log_util');

exports.testMongod = async () => {
  ProjectJson.findOne({ projectDesignId: 'aa4d518a-afaf-4c91-85bd-0549713d946b' }, (err, project) => {
    if (err) {
      logUtil.error('findOne project error ', err);
    } else {
      logUtil.info('findOne project is ', project);
    }
  });

  ProjectJson.find({ userId: '26' }, (err, project) => {
    if (err) {
      logUtil.error('find project error ', err);
    }
    logUtil.info('find project is ', project);
  });
};

exports.testMongoGridFs = () => {
  const readStream = gridFs().createReadStream({
    filename: 'test1.json',
    root: 'model'
  });
  const writeStream = fs.createWriteStream('E:/test1.json');
  readStream.pipe(writeStream);
};

exports.testDecode = () => {
  let jpgCode = '';
  jpgCode = jpgCode.replace(/^data:image\/\w+;base64,/, '');
  const dataBuffer = new Buffer(jpgCode, 'base64');
  fs.writeFile('E:/out.png', dataBuffer, (err) => {
    if (err) {
      logUtil.error('write png error ', err);
    } else {
      logUtil.info('保存成功！', dataBuffer);
    }
  });
};

// testFromJava请求
exports.testFromJava = async (ctx) => {
  logUtil.info('ctx request body is ', ctx.request.body);
  ctx.body = {
    status: 1,
    message: 'test response'
  };
};
